﻿using BLL;
using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS._6.ZhangWuGL
{
    public partial class DuoShouZZ : BasePage
    {
        #region 属性
        /// <summary>
        /// 用户号
        /// </summary>
        private string YONGHUH
        {
            get
            {
                return ViewState["_YONGHUH"].ToString();
            }
            set
            {
                ViewState["_YONGHUH"] = value;
            }
        }
        /// <summary>
        /// 用户欠费信息数据集
        /// </summary>
        private DataTable dtQianFei
        {
            get
            {
                return ViewState["_dtQianFei"] as DataTable;
            }
            set
            {
                ViewState["_dtQianFei"] = value;
            }
        }
        private DataTable dtZhuanChu
        {
            get
            {
                return ViewState["_dtDuoShou"] as DataTable;
            }
            set
            {
                ViewState["_dtDuoShou"] = value;
            }
        }
        #endregion

        #region 变量
        /// <summary>
        /// 系统参数操作类
        /// </summary>
        private S_SETTINGSBLL _S_SETTINGBLL = new S_SETTINGSBLL();
        /// <summary>
        /// 系统参数实体
        /// </summary>
        private S_SETTINGS _S_SETTINGS;
        /// <summary>
        /// 用户信息BLL
        /// </summary>
        private YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();
        /// <summary>
        /// 账务信息BLL
        /// </summary>
        private QS_ZW_YINGYEZBLL _QS_ZW_YINGYEZBLL = new QS_ZW_YINGYEZBLL();
        /// <summary>
        /// 数据访问操作类
        /// </summary>
        private SpHelper _spHelper = new SpHelper();
        /// <summary>
        /// 日志操作类
        /// </summary>
        private SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        #endregion

        #region 方法
        private void InitControl()
        {
            this.txtZhangWuNY.Enabled = false;
            this.txtYongHuM.Enabled = false;
            this.txtDuoShouJE.Enabled = false;
            this.txtYongHuH.Focus();
        }
        private void InitData()
        {
            _S_SETTINGS = _S_SETTINGBLL.GetCurrentZhangWuNY();
            this.txtZhangWuNY.Text = _S_SETTINGS.CONFIGVALUE;
            GetDuoShouZZSJ();
        }
        /// <summary>
        /// 获取多收转账数据
        /// </summary>
        private void GetDuoShouZZSJ()
        {
            int _ZhangWuNY = Convert.ToInt32(this.txtZhangWuNY.Text.Trim());
            DataTable dt = _spHelper.P_QS_ZW_GETDUOSHOUZZSJ(_ZhangWuNY).Tables[0];
            this.dtZhuanChu = dt;
            this.gvQuXiaoDSZZ.DataSource = this.dtZhuanChu;
            this.gvQuXiaoDSZZ.DataBind();
        }
        /// <summary>
        /// 设置编辑控件状态
        /// </summary>
        private void SetOperationData()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    this.txtShuiLiangNY.Enabled = false;
                    this.txtZhuanChuJE.Enabled = false;
                    this.txtYingShouJE.Enabled = false;
                    this.txtQianFeiJE.Enabled = false;
                    this.btnZhuanChu.Enabled = false;
                    break;
                case GridViewStatus.Update:
                    this.txtShuiLiangNY.Enabled = true;
                    this.txtZhuanChuJE.Enabled = true;
                    this.txtYingShouJE.Enabled = false;
                    this.txtQianFeiJE.Enabled = false;
                    this.btnZhuanChu.Enabled = true;
                    break;
            }
        }
        /// <summary>
        /// 查找用户信息
        /// </summary>
        /// <returns></returns>
        private bool SearchYongHuXX()
        {
            string _YongHuH = this.txtYongHuH.Text.Trim();
            bool _Result = false;
            if (string.IsNullOrEmpty(_YongHuH))
            {
                PageTools.AlertMsg(this, "请输入用户号！");
            }
            else
            {
                string _Where = "I_JLZT = 0 and S_YongHuH='" + _YongHuH + "'";
                DataTable dt = _YS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(_Where, "");
                if ((dt == null) || (dt.Rows.Count == 0))
                {
                    PageTools.AlertMsg(this, "用户号不存在，请重新输入！");
                }
                else
                {
                    DataRow dr = dt.Rows[0];
                    this.txtYongHuM.Text = dr["S_YongHuM"].ToString();
                    this.YONGHUH = _YongHuH;
                    _Result = true;
                }
            }
            return _Result;
        }
        /// <summary>
        /// 查找多收信息、加载欠费信息
        /// </summary>
        /// <returns></returns>
        private bool SearchZhangWuXX()
        {
            bool _Result = false;
            decimal _Amount = 0;
            string _Where = "I_JLZT = 0 and S_YongHuH = '" + this.YONGHUH + "' and (N_ShiShouJE - N_YingShouJE > 0)";
            DataTable dt = _QS_ZW_YINGYEZBLL.SelectQS_ZW_YINGYEZsDynamic(_Where, "");
            if ((dt == null) || (dt.Rows.Count == 0))
            {
                PageTools.AlertMsg(this, "当前用户没有多收金额！");
            }
            else
            {
                foreach (DataRow dr in dt.Rows)
                {
                    _Amount += Convert.ToDecimal(dr["N_ShiShouJE"]) - Convert.ToDecimal(dr["N_YingShouJE"]);
                }
                _Result = true;
            }
            this.txtDuoShouJE.Text = string.Format("{0:N2}", _Amount);
            //加载欠费信息
            dt = _spHelper.P_YSSF_GETQIANFEIJL(this.YONGHUH, null, null).Tables[0];
            if ((dt == null) || (dt.Rows.Count == 0))
            {
                PageTools.AlertMsg(this, "当前用户没有欠费信息！");
            }
            this.dtQianFei = dt;
            return _Result;
        }
        /// <summary>
        /// 查找欠费信息
        /// </summary>
        /// <returns></returns>
        private bool SearchQianFeiXX()
        {
            bool _Result = false;
            string _ShuiLiangNY = txtShuiLiangNY.Text.Trim();
            if (_ShuiLiangNY.Length != 6)
            {
                PageTools.AlertMsg(this, "请输入正确的水量年月！");
                return _Result;
            }
            if (!PageTools.ValidateString(_ShuiLiangNY, PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this, "请输入正确的水量年月！");
                return _Result;
            }
            int _ShuiLiangN = Convert.ToInt32(_ShuiLiangNY) / 100;
            int _ShuiLiangY = Convert.ToInt32(_ShuiLiangNY) % 100;
            DataTable dt = _spHelper.P_QSSF_GETQIANFEIJL(this.YONGHUH, _ShuiLiangN, _ShuiLiangY).Tables[0];
            if ((dt == null) || (dt.Rows.Count == 0))
            {
                PageTools.AlertMsg(this, "当前用户该水量年月水费已缴清！");
                return _Result;
            }
            decimal _YingShouJE = Convert.ToDecimal(dt.Rows[0]["N_YINGSHOUJE"]);
            decimal _QianFeiJE = Convert.ToDecimal(dt.Rows[0]["N_QIANFEIJE"]);
            decimal _ZhuanChuJE = _QianFeiJE;
            decimal _DuoShouJE = Convert.ToDecimal(txtDuoShouJE.Text);
            if (_ZhuanChuJE > _DuoShouJE)
            {
                _ZhuanChuJE = _DuoShouJE;
            }
            this.txtYingShouJE.Text = string.Format("{0:N2}", _YingShouJE);
            this.txtQianFeiJE.Text = string.Format("{0:N2}", _QianFeiJE);
            if (string.IsNullOrEmpty(this.txtZhuanChuJE.Text.Trim()))
            {
                this.txtZhuanChuJE.Text = string.Format("{0:N2}", _ZhuanChuJE);
            }
            _Result = true;
            return _Result;
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                InitData();
            }
        }

        protected void btnZhuanChu_Click(object sender, EventArgs e)
        {
            if (!SearchQianFeiXX())
            {
                this.txtShuiLiangNY.Focus();
                return;
            }
            //对转出金额做检查
            if (!PageTools.ValidateString(this.txtZhuanChuJE.Text.Trim(), PageTools.eValidateType.水价))
            {
                PageTools.AlertMsg(this, "请填写正确的转出金额！");
                return;
            }
            decimal _ZhuanChuJE = Convert.ToDecimal(this.txtZhuanChuJE.Text.Trim());
            decimal _QianFeiJE = Convert.ToDecimal(this.txtQianFeiJE.Text.Trim());
            decimal _DuoShouJE = Convert.ToDecimal(this.txtDuoShouJE.Text.Trim());
            if (_ZhuanChuJE > _DuoShouJE)
            {
                PageTools.AlertMsg(this, "转出金额不能大于多收金额！");
                return;
            }
            if (_ZhuanChuJE > _QianFeiJE)
            {
                PageTools.AlertMsg(this, "转出金额不能大于欠费金额！");
                return;
            }
            PageTools.ConfirmMsg(this, "确认要对当前用户做多收转出操作吗？", "btnZhuanChuFact");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            bool _Result = false;
            this.Status = GridViewStatus.Init;
            if (SearchYongHuXX() && SearchZhangWuXX())
            {
                _Result = true;
            }
            else
            {
                this.txtDuoShouJE.Text = "";
                this.dtQianFei = null;
            }
            this.txtShuiLiangNY.Text = "";
            this.txtZhuanChuJE.Text = "";
            this.gvQianFeiXX.DataSource = this.dtQianFei;
            this.gvQianFeiXX.DataBind();

            if (dtQianFei.Rows.Count == 0) 
            {
                btnZhuanChu.Visible = false;
            }
            if (_Result)
            {
                this.Status = GridViewStatus.Update;
            }
            SetOperationData();
            if (_Result)
            {
                this.txtShuiLiangNY.Focus();
            }
            else
            {
                this.txtYongHuH.Focus();
            }
        }
        protected void btnSearchQianF_Click(object sender, EventArgs e)
        {

            if (SearchQianFeiXX())
            {
                this.txtZhuanChuJE.Focus();
            }
            else
            {
                this.txtShuiLiangNY.Focus();
            }
        }
        protected void btnQuXiaoDSZZ_Command(object sender, CommandEventArgs e)
        {
            int? _DuoShouZZID = Convert.ToInt32(e.CommandArgument);
            int? _ErrCode = -1;
            string _ErrMsg = "";
            _spHelper.P_QS_ZW_QUXIAODSZZ(_DuoShouZZID, LoginInfo.CurrentUser.UserID, out _ErrCode, out _ErrMsg);
            DBResult _dsResult = new DBResult { RSNO = _ErrCode.ToString(), RSMSG = _ErrMsg };
            if (_dsResult.IsSuccess)
            {
                PageTools.AlertMsg(this, "取消转出成功！");
                //重新载入转出信息
                GetDuoShouZZSJ();
            }
            else
            {
                PageTools.AlertMsg(this, "取消转出失败: " + _ErrMsg);
            }
        }
        protected void btnZhuanChuFact_Click(object sender, EventArgs e)
        {
            if (!SearchQianFeiXX())
            {
                this.txtShuiLiangNY.Focus();
                return;
            }
            int? _ErrCode = -1;
            string _ErrMsg = "";
            _spHelper.P_QS_SF_DUOSHOUZZ(this.YONGHUH, Convert.ToInt32(this.txtZhangWuNY.Text.Trim()), Convert.ToInt32(this.txtShuiLiangNY.Text.Trim()),
                   LoginInfo.CurrentUser.UserID, Convert.ToDecimal(this.txtZhuanChuJE.Text.Trim()), out _ErrCode, out _ErrMsg);
            DBResult _dsResult = new DBResult { RSNO = _ErrCode.ToString(), RSMSG = _ErrMsg };
            if (_dsResult.IsSuccess)
            {
                PageTools.AlertMsg(this, "金额转出成功！");
                //重新载入多收金额、欠费信息、转出信息
                btnSearch_Click(sender, e);
                GetDuoShouZZSJ();
            }
            else
            {
                PageTools.AlertMsg(this, "金额转出失败: " + _ErrMsg);
            }
        }
        #endregion
    }
}